﻿using Travel.Data.Inputs;
using Travel.Data;
using Microsoft.AspNetCore.Mvc;
using Travel.API.Filters;
using Travel.Service.TravelBusiness;
using DotNetCore.CAP;
using Travel.Data.Consts;
using Microsoft.AspNetCore.SignalR;
using Travel.API.Hubs;
using Travel.Data.Dtos.TravelBusiness;
using Travel.Data.Dtos;

namespace Travel.API.Controller
{
    /// <summary>
    /// 订单
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class OrderController : BaseController
    {
        private readonly IOrderService _orderService;
        private readonly IHubContext<TravelHubs> _hubContext;
        public OrderController(IOrderService orderService, IHubContext<TravelHubs> hubContext)
        {
            _orderService = orderService;
            _hubContext = hubContext;
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [CapSubscribe(EventBus.OrderAdd, Group = EventBus.OrderAdd)]
        [NonAction]
        public async Task OrderAdd([FromBody] OrderAddInput input)
        {
            var results = await _orderService.OrderAdd(input);
            await _hubContext.Clients.Client(input.ConnectionId).SendAsync(SignalMethod.ReceiveMessage, results);
        }
        /// <summary>
        /// 订单查询-管理员端
        /// </summary>
        /// <param name="input"></param>
        /// <remarks>管理员调用</remarks>
        /// <returns></returns> 
        [HttpPost]
        [TravelAuthorize(AuthorizeRoleName.Administrator)]
        public async Task<Results<PageDto<OrderDto>>> OrderQuery([FromBody]OrderQueryInput input)
        {
            return await _orderService.OrderQuery(input);
        }
        /// <summary>
        /// 订单查询-个人端
        /// </summary>
        /// <param name="input"></param>
        /// <remarks>个人中心调用</remarks>
        /// <returns></returns> 
        [HttpPost]
        [TravelAuthorize]
        public async Task<Results<PageDto<OrderDto>>> UserOrderQuery([FromBody] OrderQueryInput input)
        {
            input.LoginUserId = LoginUserId;
            return await _orderService.OrderQuery(input);
        }
        /// <summary>
        /// 订单查询-商家端 
        /// </summary>
        /// <param name="input"></param>
        /// <remarks>商家调用</remarks>
        /// <returns></returns> 
        [HttpPost]
        [TravelAuthorize(AuthorizeRoleName.SellerAdministrator)]
        public async Task<Results<PageDto<OrderDto>>> SellerOrderQuery([FromBody] OrderQueryInput input)
        {
            input.SellerUserId = LoginUserId;
            return await _orderService.OrderQuery(input);
        }
    }
}
